package com.burstly.lib.service;

import android.content.Context;
import com.burstly.lib.network.request.DefaultRequestCallback;
import com.burstly.lib.network.request.RequestManager;
import com.burstly.lib.network.request.RequestManager.RequestDataObject;
import com.burstly.lib.util.LoggerExt;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

final class IpDownloader
{
  private static final List<String> EMPTY_ARRAY_LIST = new ArrayList();
  private static final LoggerExt LOG = LoggerExt.getInstance();
  private static final long MILLISEC_IN_DAY = 86400000L;
  private static final String TAG = "IpDownloader";
  private File mCacheFile;
  private Context mContext;
  final String mFileName;
  private final IHostProvider mHostProvider;
  volatile boolean mInProgress;
  private final List<String> mIpList;
  final String mUri;

  IpDownloader(Context paramContext, String paramString1, String paramString2, IHostProvider paramIHostProvider)
  {
    this.mFileName = paramString1;
    this.mUri = paramString2;
    this.mHostProvider = paramIHostProvider;
    this.mIpList = new ArrayList();
    if (paramContext != null)
    {
      this.mContext = paramContext.getApplicationContext();
      return;
    }
    LOG.logInfo("IpDownloader", "Can't run server list update with null context.", new Object[0]);
  }

  private RequestManager.RequestDataObject<String> createRequestTO()
  {
    return new RequestManager.RequestDataObject(this.mUri, null, "IP list request", new IpListCallback(this), String.class, this.mHostProvider);
  }

  private boolean loadIpsIfNeed()
  {
    if (this.mContext == null)
      return false;
    readCacheFile(this.mContext);
    long l1;
    long l2;
    if (this.mCacheFile != null)
    {
      l1 = this.mCacheFile.lastModified();
      l2 = l1 + 86400000L;
      if (System.currentTimeMillis() <= l2)
        break label106;
      if (this.mInProgress)
        break label88;
      LOG.logInfo("IpDownloader", "Ip cache file is stale. Downloading actual...", new Object[0]);
      this.mInProgress = true;
      RequestManager.makeIpListRequest(createRequestTO());
    }
    while (true)
    {
      return false;
      l1 = 0L;
      break;
      label88: LOG.logDebug("IpDownloader", "Request of default Burslty servers is in progress...", new Object[0]);
    }
    label106: LoggerExt localLoggerExt = LOG;
    Object[] arrayOfObject = new Object[1];
    arrayOfObject[0] = new Date(l2);
    localLoggerExt.logInfo("IpDownloader", "Ip cache file is actual. Expiration date is: {0}", arrayOfObject);
    return true;
  }

  private void readCacheFile(Context paramContext)
  {
    File localFile = paramContext.getFileStreamPath(this.mFileName);
    if (localFile.exists())
      this.mCacheFile = localFile;
  }

  private List<String> readCachedList()
  {
    List localList = readList();
    if (!localList.isEmpty())
    {
      LOG.logDebug("IpDownloader", "Retrieved {0} from cache ip file.", new Object[] { localList });
      this.mIpList.addAll(localList);
    }
    while (true)
    {
      return this.mIpList;
      loadIpsIfNeed();
      LoggerExt localLoggerExt = LOG;
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = EMPTY_ARRAY_LIST;
      localLoggerExt.logDebug("IpDownloader", "Retrieved {0} because of stale ip file.", arrayOfObject);
    }
  }

  // ERROR //
  private List<String> readList()
  {
    // Byte code:
    //   0: aconst_null
    //   1: astore_1
    //   2: aconst_null
    //   3: astore_2
    //   4: aload_0
    //   5: getfield 63	com/burstly/lib/service/IpDownloader:mContext	Landroid/content/Context;
    //   8: astore 7
    //   10: aconst_null
    //   11: astore_1
    //   12: aconst_null
    //   13: astore_2
    //   14: aload 7
    //   16: ifnull +79 -> 95
    //   19: aload_0
    //   20: getfield 63	com/burstly/lib/service/IpDownloader:mContext	Landroid/content/Context;
    //   23: aload_0
    //   24: getfield 49	com/burstly/lib/service/IpDownloader:mFileName	Ljava/lang/String;
    //   27: invokevirtual 162	android/content/Context:openFileInput	(Ljava/lang/String;)Ljava/io/FileInputStream;
    //   30: astore_1
    //   31: new 164	java/io/InputStreamReader
    //   34: dup
    //   35: aload_1
    //   36: invokespecial 167	java/io/InputStreamReader:<init>	(Ljava/io/InputStream;)V
    //   39: astore 8
    //   41: sipush 1024
    //   44: newarray char
    //   46: astore 10
    //   48: aload 8
    //   50: aload 10
    //   52: invokevirtual 171	java/io/InputStreamReader:read	([C)I
    //   55: pop
    //   56: new 83	java/lang/String
    //   59: dup
    //   60: aload 10
    //   62: invokespecial 174	java/lang/String:<init>	([C)V
    //   65: invokestatic 180	com/burstly/lib/util/Utils:fromJsonToListOfStrings	(Ljava/lang/String;)Ljava/util/List;
    //   68: astore 12
    //   70: aload 12
    //   72: astore 6
    //   74: aload_1
    //   75: ifnull +7 -> 82
    //   78: aload_1
    //   79: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   82: aload 8
    //   84: ifnull +8 -> 92
    //   87: aload 8
    //   89: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   92: aload 6
    //   94: areturn
    //   95: getstatic 45	com/burstly/lib/service/IpDownloader:LOG	Lcom/burstly/lib/util/LoggerExt;
    //   98: ldc 17
    //   100: ldc 186
    //   102: iconst_0
    //   103: anewarray 4	java/lang/Object
    //   106: invokevirtual 69	com/burstly/lib/util/LoggerExt:logInfo	(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;
    //   109: pop
    //   110: getstatic 37	com/burstly/lib/service/IpDownloader:EMPTY_ARRAY_LIST	Ljava/util/List;
    //   113: astore 6
    //   115: iconst_0
    //   116: ifeq +7 -> 123
    //   119: aconst_null
    //   120: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   123: iconst_0
    //   124: ifeq -32 -> 92
    //   127: aconst_null
    //   128: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   131: aload 6
    //   133: areturn
    //   134: astore 4
    //   136: getstatic 45	com/burstly/lib/service/IpDownloader:LOG	Lcom/burstly/lib/util/LoggerExt;
    //   139: ldc 17
    //   141: ldc 188
    //   143: iconst_0
    //   144: anewarray 4	java/lang/Object
    //   147: invokevirtual 122	com/burstly/lib/util/LoggerExt:logDebug	(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;
    //   150: pop
    //   151: getstatic 37	com/burstly/lib/service/IpDownloader:EMPTY_ARRAY_LIST	Ljava/util/List;
    //   154: astore 6
    //   156: aload_1
    //   157: ifnull +7 -> 164
    //   160: aload_1
    //   161: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   164: aload_2
    //   165: ifnull -73 -> 92
    //   168: aload_2
    //   169: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   172: aload 6
    //   174: areturn
    //   175: astore_3
    //   176: aload_1
    //   177: ifnull +7 -> 184
    //   180: aload_1
    //   181: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   184: aload_2
    //   185: ifnull +7 -> 192
    //   188: aload_2
    //   189: invokestatic 184	com/burstly/lib/util/Utils:closeStream	(Ljava/io/Closeable;)V
    //   192: aload_3
    //   193: athrow
    //   194: astore_3
    //   195: aload 8
    //   197: astore_2
    //   198: goto -22 -> 176
    //   201: astore 9
    //   203: aload 8
    //   205: astore_2
    //   206: goto -70 -> 136
    //
    // Exception table:
    //   from	to	target	type
    //   4	10	134	java/io/IOException
    //   19	41	134	java/io/IOException
    //   95	115	134	java/io/IOException
    //   4	10	175	finally
    //   19	41	175	finally
    //   95	115	175	finally
    //   136	156	175	finally
    //   41	70	194	finally
    //   41	70	201	java/io/IOException
  }

  List<String> getIpList()
  {
    LOG.logDebug("IpDownloader", "Retrieving Burslty servers...", new Object[0]);
    if (this.mIpList.isEmpty())
      return readCachedList();
    if (!loadIpsIfNeed())
    {
      this.mIpList.clear();
      LoggerExt localLoggerExt2 = LOG;
      Object[] arrayOfObject2 = new Object[1];
      arrayOfObject2[0] = EMPTY_ARRAY_LIST;
      localLoggerExt2.logDebug("IpDownloader", "Retrieved {0} because of stale ip file.", arrayOfObject2);
      return EMPTY_ARRAY_LIST;
    }
    LoggerExt localLoggerExt1 = LOG;
    Object[] arrayOfObject1 = new Object[1];
    arrayOfObject1[0] = this.mIpList;
    localLoggerExt1.logDebug("IpDownloader", "Retrieved {0} from ip file cache.", arrayOfObject1);
    return new ArrayList(this.mIpList);
  }

  void saveIPsToFile(String paramString)
  {
    try
    {
      if ((!paramString.equals("")) && (!paramString.equals("[]")))
      {
        if (this.mContext != null)
        {
          OutputStreamWriter localOutputStreamWriter = new OutputStreamWriter(this.mContext.openFileOutput(this.mFileName, 0));
          localOutputStreamWriter.write(paramString);
          localOutputStreamWriter.flush();
          localOutputStreamWriter.close();
          this.mCacheFile = this.mContext.getFileStreamPath(this.mFileName);
        }
        while (true)
        {
          this.mInProgress = false;
          return;
          LOG.logInfo("IpDownloader", "Can't save ip addresses with null context passed. Call updateList first!", new Object[0]);
        }
      }
    }
    catch (IOException localIOException)
    {
      while (true)
      {
        LOG.logThrowable("IpDownloader", localIOException);
        continue;
        LOG.logInfo("IpDownloader", "Can't save ip addresses because of the servers list is empty!", new Object[0]);
      }
    }
  }

  private static class IpListCallback extends DefaultRequestCallback<String>
  {
    private final IpDownloader mIpService;

    IpListCallback(IpDownloader paramIpDownloader)
    {
      this.mIpService = paramIpDownloader;
    }

    public void onFailInBackground(String paramString)
    {
      IpDownloader.LOG.logWarning("IpDownloader", "Could not download server ip list. Using default.", new Object[0]);
      this.mIpService.mInProgress = false;
    }

    public void onNoConnection()
    {
      this.mIpService.mInProgress = false;
    }

    public void onSuccessInBackground(String paramString)
    {
      this.mIpService.saveIPsToFile(paramString);
    }
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.service.IpDownloader
 * JD-Core Version:    0.6.2
 */